<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="项目标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('sys:conclusion:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('sys:conclusion:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-radio-group v-model="dataForm.conclusionPostpone">
          <el-radio-button :label="null">{{ '全部' }}</el-radio-button>
          <el-radio-button label="结题">{{ '结题' }}</el-radio-button>
          <el-radio-button label="延期">{{ '延期' }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="showExcel = true" :disabled="excelData.length == 0">{{ '导出' }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-dialog :visible.sync="showExcel" modal-append-to-body append-to-body width="50%">
          <div>
            <h3>输入excel基本信息</h3>
            <el-form class="excelForm" :label-position="'left'">
              <el-form-item label="excel表标题:">
                <el-input size="small" placeholder="表格标题" style="width:160px" v-model="excelTitle"></el-input>
              </el-form-item>
              <el-form-item label="excel文件名:">
                <el-input size="small" placeholder="文件名" style="width:160px" v-model="excelFName">
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <div>
            <h3>选要导出的字段</h3>
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkExcelAll"
              @change="handleCheckAllChange">全选</el-checkbox>
            <el-checkbox-group v-model="checked_key" @change="handleCheckedKeyChange"
              style="display:flex ; align-items: center;flex-wrap: wrap; justify-content: center;">
              <el-checkbox v-for="k in handleJson_fields" :label="k" :key="k.key">{{ k.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
          <span slot="footer" class="dialog-footer" style="display: flex;justify-content: flex-end;">
            <el-button @click="showExcel = false">取 消</el-button>
            <!-- <el-button type="primary" @click="showExcel = false">确 定</el-button> -->
            <download-excel :data="excelData" :fields="header" :header="excelTitle" :name="excelFName"
              :before-generate='beforeGenerate'>
              <el-button v-if="(excelTitle != '' && excelFName != '' && checked_key.length != 0)" icon="el-icon-download"
                style="margin: 0 10px;" type="primary">
                {{ "立即导出" }}
              </el-button>
              <el-tag v-else disable-transitions style="margin: 0 0 0 5px; height: 36px;line-height: 36px;">{{
                '填完上面的东西显示导出按钮' }}</el-tag>
            </download-excel>
          </span>
        </el-dialog>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="项目标题">
      </el-table-column>
      <el-table-column prop="projectType" header-align="center" align="center" label="大赛类型">
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="项目类型">
      </el-table-column>
      <el-table-column prop="leader" header-align="center" align="center" label="负责人">
      </el-table-column>
      <el-table-column prop="college" header-align="center" align="center" label="学院">
      </el-table-column>
      <el-table-column prop="passCount" header-align="center" align="center" label="通过数">
        <template slot-scope="scope">
          <div>{{ scope.row.passCount ? scope.row.passCount : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="noPass" header-align="center" align="center" label="不通过数">
        <template slot-scope="scope">
          <div>{{ scope.row.noPass ? scope.row.noPass : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="peopleNumber" header-align="center" align="center" label="评审人数">
        <template slot-scope="scope">
          <div>{{ scope.row.peopleNumber ? scope.row.peopleNumber : 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="level" header-align="center" align="center" label="校评级">
      </el-table-column>
      <el-table-column prop="conclusionPostpone" header-align="center" align="center" label="结题/延期">
        <template slot-scope="scope">
          <div>
            <el-tag :type="scope.row.conclusionPostpone == '结题' ? '' : 'danger'">{{ scope.row.conclusionPostpone
            }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="statu" header-align="center" align="center" label="验收状态">
        <template slot-scope="scope">
          <!-- <div v-if="scope.row.statu !== 0">{{ backStatu(scope.row.statu) }}</div> -->
          <div>
            <el-select v-if="scope.row.statu == 0" v-model="scope.row.statu" placeholder="请选择"
              @change="handleStatu(scope.row.statu, scope.row.title, scope.row.id, scope.row.conclusionPostpone)">
              <el-option
                v-for="k in scope.row.conclusionPostpone === '结题' ? [statuOptions[0], statuOptions[1], statuOptions[2]] : [statuOptions[0], statuOptions[3], statuOptions[4]]"
                :key="k.statu" :label="k.label" :value="k.statu">
              </el-option>
            </el-select>
            <el-tag type="danger" v-else-if="scope.row.statu === 5 || scope.row.statu === 6">{{ '被打回' }}</el-tag>
            <el-tag v-else disable-transitions :type="scope.row.statu != 0 && scope.row.statu % 2 ? '' : 'danger'">
              {{ backStatu(scope.row.statu) }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column style="" fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <el-button type="text" size="small" @click="repulse(scope.row.id, scope.row.conclusionPostpone)">打回</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
// import func from 'vue-editor-bridge'
import AddOrUpdate from './conclusion-add-or-update'
export default {
  data() {
    return {
      dataForm: {
        key: '',
        // type: '结题',
        conclusionPostpone: null,
      },
      statuOptions: [{
        label: '未验收',
        statu: 0
      }, {
        label: '验收合格',
        statu: 1
      }, {
        label: '验收不合格',
        statu: 2
      }, {
        label: '同意延期',
        statu: 3
      }, {
        label: '不同意延期',
        statu: 4
      }
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,

      //导出excel全选
      checkExcelAll: false,
      //控制全选框的样式
      isIndeterminate: false,
      showExcel: false,
      excelTitle: '',
      excelFName: '',
      excelData: [],
      //已选择的字段
      checked_key: [],
      // 供选择的字段
      handleJson_fields: [
        {
          name: '项目标题',
          key: "title",
        },
        {
          name: '项目类型',
          key: 'type',
        },
        {
          name: '大赛类型',
          key: 'projectType',
        },
        {
          name: '学院',
          key: 'college',
        },
        // {
        //   name: '项目概要',
        //   key: 'outline',
        // },
        // {
        //   name: '指导老师',
        //   key: 'teacherName',
        // },
        {
          name: '负责人',
          key: 'leader',
        },
        {
          name: '负责人联系方式',
          key: 'phone',
        },
        {
          name: "不通过数",
          key: 'nopass',
        },
        {
          name: "通过数",
          key: 'passCount',
        },
        {
          name: '结题/延期',
          key: 'conclusionPostpone',
        },
        // {
        //   name: '校评审平均分',
        //   key: 'average',
        // },
        // {
        //   name: '评分人数',
        //   key: 'peopleNumber',
        // },
        {
          name: '校推荐意见',
          key: 'level',
        },
        {
          name: "验收状态",
          key: "statu",
        }

        // {
        //   name: '院推荐意见',
        //   key: 'ranks',
        // },
        // {
        //   name: '院评审平均分',
        //   key: 'collegeAverage',
        // },

      ],
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  watch: {
    'dataForm.conclusionPostpone'(newV) {
      // console.log(newV);
      this.getDataList()
    }
  },
  computed: {
    header() {
      let obj = {}
      for (let i = 0; i < this.checked_key.length; i++) {
        if (this.checked_key[i].key === 'noPass') {
          obj[this.checked_key[i].name] = {
            field: this.checked_key[i].key,
            callback: (v) => {
              if (v === 0)
                return '无'
              else
                return v
            }

          }
        }
        if (this.checked_key[i].key === 'passCount') {
          obj[this.checked_key[i].name] = {
            field: this.checked_key[i].key,
            callback: (v) => {
              if (v === 0)
                return '无'
              else
                return v
            }

          }
        }
        if (this.checked_key[i].key === 'statu') {
          obj[this.checked_key[i].name] = {
            field: this.checked_key[i].key,
            callback: value => {
              if (value == 0) {
                return '未验收'
              } else if (value == 1) {
                return '验收合格'
              } else if (value == 2) {
                return '验收不合格'
              } else if (value == 3) {
                return '同意延期'
              } else if (value == 4) {
                return '不同意延期'
              }
            }
          }
        } else {
          obj[this.checked_key[i].name] = this.checked_key[i].key
        }

      }
      return obj
    },
  },
  methods: {
    //拿导出表
    downExcel() {
      var ids = this.dataListSelections.map(item => {
        return item.id
      })
      this.$http({
        url: this.$http.adornUrl('/sys/conclusion/downExcel'),
        method: 'post',
        data: ids,
        // data: this.$http.adornData({ ids, })
        // conclusionPostpone: this.dataForm.conclusionPostpone
      }).then(({
        data
      }) => {
        // console.log(data);
        if (data && data.code === 0) {
          this.excelData = data.conclusionEntityList
        } else {
          this.$message('数据错误，请联系管理员')
        }
      })
    },
    handleCheckedKeyChange(value) {
      let checkedCount = value.length
      this.checkExcelAll = checkedCount === this.handleJson_fields.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.handleJson_fields.length
    },
    handleCheckAllChange(val) {
      this.checked_key = val ? this.handleJson_fields : []
      this.isIndeterminate = false
    },
    beforeGenerate() {
      // console.log(e,'excel');//sb插件，这个钩子没有event
      this.$message('开始导出文档')
    },


    repulse(id, conclusionPostpone) {
      this.$confirm(`你确定要将该项目的${conclusionPostpone}申请打回吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {

        this.$http({
          url: this.$http.adornUrl(`/sys/conclusion/refer2`),
          method: 'post',
          data: this.$http.adornData({
            id,
            roleId: parseInt(this.$cookie.get('roleId'))
          })
        }).then(({ data }) => {
          this.getDataList()
          if (data.code === 0) {
            this.$message({
              type: 'success',
              message: '打回成功',
            })
          } else {
            // console.log(data);
            this.$message(data.msg)
          }

        })

      }).catch((e) => {
        if (e === 'cancel')
          return
      })

    },
    //改变验收状态
    referStatu(statu, id, conclusionPostpone) {
      this.$http({
        url: this.$http.adornUrl(`/sys/conclusion/refer`),
        method: 'post',
        data: this.$http.adornData({
          statu,
          // id: id.toString(),
          id,
          conclusionPostpone,
          roleId: parseInt(this.$cookie.get('roleId'))
        })
      }).then((res) => {
        // console.log(res);
        if (res.data && res.data.code == 0) {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: '修改失败'
          })
        }
      })
    }
    ,
    handleStatu(statu, title, id, conclusionPostpone) {
      console.log(statu);
      let char = '你确定要将项目:' + '<br/>' + '<strong style="color:#409EFF; text-align: center;">' + title + '</strong>' + '<br/>' + '的状态改为' + '<strong style="color:red">' + this.backStatu(statu) + '</strong>' + '吗？'
      this.$confirm(char, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        this.referStatu(statu, id, conclusionPostpone)
        this.getDataList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.getDataList()
      })
    },
    backStatu(s) {
      switch (s) {
        case 0: return '未验收';
        case 1: return '验收合格';
        case 2: return '验收不合格';
        case 3: return '同意延期';
        case 4: return '不同意延期';

      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/conclusion/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key,
          'conclusionPostpone': this.dataForm.conclusionPostpone
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
      this.excelData = val
      if (this.dataListSelections.length != 0) {
        this.downExcel()
        // this.downExcelOfMember()
      }
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/conclusion/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
